package com.house.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.house.common.R;
import com.house.entity.Building;
import com.house.service.BuildingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    /**
     * 保存建筑三
     * @param building
     * @return
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody Building building) {
        buildingService.save(building);
        return R.success("添加成功!");
    }

    /**
     * 通过ID查找建筑
     * @param buildingId
     * @return
     */
    @GetMapping("/getBuildingByBuildingId/{buildingId}")
    public R<Building> getBuildingByBuildingId(@PathVariable Long buildingId){
        Building building = buildingService.selectBuildingById(buildingId);
        return R.success(building);
    }
    /**
     * 更新建筑
     * @param building
     * @return
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Building building) {
        buildingService.updateBuildingById(building);
        return R.success("修改成功!");
    }

    /**
     * 删除建筑
     * @param buildingId
     * @return
     */
    @DeleteMapping("/deleteBuildingByBuildingId/{buildingId}")
    public R<String> delete(@PathVariable Long buildingId) {
        int res = buildingService.delete(buildingId);
        if (res == 0) {
            return R.error("删除失败!");
        }
        return R.success("删除成功!");
    }

    /**
     * 获取建筑分页
     * @param page
     * @param pageSize
     * @param buildingName
     * @return
     */
    @GetMapping("/getBuildings")
    public R<Page<Building>> getBuildings(int page, int pageSize, int type, long startAreaId, long endAreaId, String buildingName) {
        Page<Building> pageInfo = buildingService.selectPage(page, pageSize, type, startAreaId, endAreaId, buildingName);
        return R.success(pageInfo);
    }
}
